<?php
/**
 * Created by PhpStorm.
 * User: zzw
 * Date: 17-3-16
 * Time: 下午4:43
 */
class praise extends API_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->_auto();
        $this->load->model('praise_model', 'mpraise');
        $this->load->model('long_article_model', 'mlong_article');
    }
    //添加点赞
    public function cadd() {
        $data = array(
            'uid' => $this->userinfo['id'],
            'tid' => $this->data['tid'],
            'created' => time()
        );
        $info = $this->mpraise->create_long($data);
        if ($info) {
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['content'] = $info;
        } else {
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作失败';
        }
        $this->_send_json($this->vdata);
    }
    //修改我的点赞
    public function cupdate() {
        $collect = $this->mpraise->get_one(array('uid'=>$this->userinfo['id'],'tid'=>$this->data['tid'],'is_deleted'=>1));
        if($collect) {
            $tid = $collect['tid'];
            $long = $this->mlong_article->get_one($tid,'praise');

            if($long) {
                if($collect['is_deleted']) {
                    $data = $this->mpraise->set($collect['id'],array('is_deleted'=>0));
                    $this->mlong_article->set($tid,array('praise','praise - 1'));
                } else {
                    $data = $this->mpraise->set($collect['id'],array('is_deleted'=>1));
                    $this->mlong_article->set($tid,array('praise','praise + 1'));
                }
                $this->vdata['returnCode'] = '200';
                $this->vdata['returnInfo'] = '操作成功';
                $this->vdata['content'] = $data;
            } else {
                $this->vdata['returnCode'] = '200';
                $this->vdata['returnInfo'] = '此长文章不存在';
            }

        } else {
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '此记录不存在';
        }
        $this->_send_json($this->vdata);
    }
    //我的收藏列表
    public function clist() {
        $where = array('praise.uid' => $this->userinfo['id']);
        $this->_list();
        $this->orderby = 'praise.created desc';
        $this->Fields = 'praise.id,praise.is_deleted,account.uname,account.photo,long_article.repeats,long_article.comments,long_article.collects,long_article.zans,long_article.title';
        if ($list = $this->mcollects->get_list_long($this->limit, $this->offset, $this->orderby, $where, $this->Fields)) {
            photo2url($list);
            //$this->mproduct->get_count_all($where);
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = $list;
        } else {
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作失败';
            $this->vdata['content'] = '';
        }
        // 返回json数据
        $this->_send_json($this->vdata);
    }
}